package view.CommonView;

import java.awt.*;

import javax.swing.*;
/**
 * @Description: 
 **/

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 自定义封装JPanel组件
 * 通过重写paintComponent方法绘制背景图片
 *
 */
public class CommonJPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int TYPE;
	public static int TYPE_BG = 0;//背景
	public static int TYPE_TRANSPARENT = 1;//透明背景
	ImageIcon icon;
	Image img;
	public CommonJPanel() {

	}
	public CommonJPanel(int type) {
	    TYPE = type;
		if(TYPE==TYPE_BG) {
			icon=new ImageIcon(getClass().getResource("/img/bg.jpg"));
			img=icon.getImage();
		}
		if(TYPE==TYPE_TRANSPARENT) {
			setOpaque(false);// 设置背景色为透明
		}
		
	}
	//先当于图形绘画的main函数，会自动执行
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(TYPE==TYPE_BG) {
			// 下面这行是为了背景图片可以跟随窗口自行调整大小，可以自己设置成固定大小
			g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
		}
		
		
	}
}
